package sunwul.hospital.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sunwul.hospital.hosp.mapper.BookingRuleMapper;
import sunwul.hospital.hosp.util.CheckUtil;
import sunwul.hospital.hosp.mapper.HospitalMapper;
import sunwul.hospital.hosp.service.HospitalService;
import sunwul.hospital.model.model.hosp.BookingRule;
import sunwul.hospital.model.model.hosp.Hospital;

import java.util.Map;

/**
 * @author sunwul
 * @date 2023/11/2 17:27:44
 * @description
 */
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper,Hospital> implements HospitalService {

    @Autowired
    CheckUtil checkUtil;

    @Autowired
    BookingRuleMapper bookingRuleMapper;

    /**
     * 上传医院
     * @param map
     */
    @Override
    @CacheEvict(value = "hospital", allEntries=true)
    public void save(Map<String, Object> map) {
        checkUtil.checkSignKey(map);
        // 取出预约规则数据, 否则JSON转换会有问题, 会将整个bookingRule值当成一个String,且首位都会加上引号, 导致转换对象报错
        String bookStr = JSONObject.toJSONString(map.get("bookingRule")).replaceFirst("\"","").replace("\\","");
        bookStr = bookStr.substring(0,bookStr.length()-1);
        String hoscode = (String) map.get("hoscode");
        map.remove("bookingRule");
        // 插入或更新数据库
        String mapJson = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapJson, Hospital.class);
        // 图片base64字符传输过程中“+”转换为了“ ”，此处要转换回来
        String logoDataString = hospital.getLogoData();
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            hospital.setLogoData(logoData);
        }
        // 存在的尝试更新,不存在的插入
        int count = baseMapper.selectCount(new QueryWrapper<Hospital>().eq("hoscode", hospital.getHoscode()));
        if (count > 0){
            baseMapper.update(hospital, new UpdateWrapper<Hospital>().eq("hoscode", hospital.getHoscode()));
        }else{
            baseMapper.insert(hospital);
        }
        // TODO 预约规则处理
        BookingRule bookingRule = JSONObject.parseObject(bookStr, BookingRule.class);
        bookingRule.setHospcode(hoscode);
        Integer book_count = bookingRuleMapper.selectCount(new QueryWrapper<BookingRule>().eq("hoscode", hospital.getHoscode()));
        if (book_count > 0){
            bookingRuleMapper.update(bookingRule, new UpdateWrapper<BookingRule>().eq("hoscode", hospital.getHoscode()));
        }else{
            bookingRuleMapper.insert(bookingRule);
        }
    }

    /**
     * 查询医院信息
     * @param map
     */
    @Override
    public Hospital getHospitalByCode(Map<String, Object> map) {
        checkUtil.checkSignKey(map);
        String hospcode = (String) map.get("hoscode");
        QueryWrapper<Hospital> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode", hospcode);
        Hospital hospital = baseMapper.selectOne(wrapper);
        BookingRule rule = bookingRuleMapper.selectOne(new QueryWrapper<BookingRule>().eq("hoscode", hospital.getHoscode()));
        hospital.setBookingRule(rule);
        return hospital;
    }

}
